package cn.hl.test;

import cn.hl.utils.ESClientUtil;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;

/**
 * 测试Java操作ES：CRUD
 */
public class ES01 {

    @Test
    public void testAdd() throws Exception{
        // 1.使用工具类获取ES的连接
        TransportClient client = ESClientUtil.getClient();
        // 2.获取添加的操作对象
        IndexRequestBuilder builder = client.prepareIndex("pethome", "pet", "1");
        // 3.准备要添加到ES的数据map对象
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",1);
        map.put("name","熊大");
        map.put("age",3);
        map.put("sex",1);
        // 4.将数据添加到ES中,得到响应结果集
        IndexResponse indexResponse = builder.setSource(map).get();
        // 5.打印
        System.out.println(indexResponse);
        // 6.关闭连接
        client.close();
    }

    @Test
    public void testGet() throws Exception{
        // 1.使用工具类获取ES的连接
        TransportClient client = ESClientUtil.getClient();
        // 2.获取查询的操作对象
        GetRequestBuilder builder = client.prepareGet("pethome", "pet", "50");
        // 3.获取指定文档
        GetResponse getResponse = builder.get();
        // 4.打印
        System.out.println(getResponse.getSource());
        // 5.关闭连接
        client.close();
    }

    @Test
    public void testUpdate() throws Exception{
        // 1.使用工具类获取ES的连接
        TransportClient client = ESClientUtil.getClient();
        // 2.获取修改的操作对象
        UpdateRequestBuilder builder = client.prepareUpdate("pethome", "pet", "1");
        // 3.准备要修改到ES的数据map对象
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","熊二");
        // 4.修改数据,得到响应结果集
        UpdateResponse updateResponse = builder.setDoc(map).get();
        // 5.打印
        System.out.println(updateResponse);
        // 6.关闭连接
        client.close();
    }

    @Test
    public void testDelete() throws Exception{
        // 1.使用工具类获取ES的连接
        TransportClient client = ESClientUtil.getClient();
        // 2.获取删除的操作对象
        DeleteRequestBuilder builder = client.prepareDelete("pethome", "pet", "1");
        // 3.执行删除
        DeleteResponse deleteResponse = builder.get();
        // 5.打印
        System.out.println(deleteResponse);
        // 6.关闭连接
        client.close();
    }

    @Test
    public void testPatchAdd() throws Exception{
        // 1.使用工具类获取ES的连接
        TransportClient client = ESClientUtil.getClient();
        for (int i = 1; i <= 50; i++) {
            // 2.获取添加的操作对象
            IndexRequestBuilder builder = client.prepareIndex("pethome", "pet", "" + i);
            // 3.准备要添加到ES的数据map对象
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",i);
            map.put("name",i % 2 == 0 ? "熊大" : "熊二");
            map.put("age",i);
            map.put("sex",i % 2);
            // 4.将数据添加到ES中,得到响应结果集
            IndexResponse indexResponse = builder.setSource(map).get();
            // 5.打印
            System.out.println(indexResponse);
        }
        // 6.关闭连接
        client.close();
    }

    /**
     * - 查询pet表
     * - name包含：熊大
     * - age在5~36之间
     * - sex=1
     * - 每页大小5
     * - 从2页开始查
     * - 按照age升序
     * @throws Exception
     */
    @Test
    public void testQuery() throws Exception{
        // 1.使用工具类获取ES的连接
        TransportClient client = ESClientUtil.getClient();
        // 2.获取查询的操作对象
        SearchRequestBuilder builder = client.prepareSearch("pethome");
        // 3.设置类型
        builder.setTypes("pet");
        // 4.指定Query搜索对象
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        // 4.1DSL查询
        boolQuery.must(QueryBuilders.matchQuery("name","熊大"));
        // 4.2DSL过滤
        boolQuery.filter(QueryBuilders.termQuery("sex",1));
        boolQuery.filter(QueryBuilders.rangeQuery("age").gte(1).lte(36));
        builder.setQuery(boolQuery);
        // 5.分页查询
        builder.setFrom(2);
        builder.setSize(5);
        // 6.排序
        builder.addSort("age", SortOrder.ASC);
        // 7.执行，得到结果集
        SearchHit[] hits = builder.get().getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsMap());
        }
        // 8.关闭连接
        client.close();
    }
}
